package com.xb.loan.cashier.biz.job;

import com.xb.loan.cashier.biz.dao.bean.CashierEncashOrder;
import com.xb.loan.cashier.biz.service.CashierEncashOrderService;
import com.xb.loan.cashier.biz.util.Configure;
import com.xb.loan.util.qrtz.task.AbstractCronTask;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;


/**1小时执行1次，监控数据库，将处理成功或失败但固定时间后(1小时)未回推的提现订单，回推给业务系统
 * 如果收银台需要部署多套，则在同一时间这个任务只能由其中一套执行
 */
public class PlatPushEncashJob extends AbstractCronTask {

    private static final Logger logger = LoggerFactory.getLogger(PlatPushEncashJob.class);
    
    private CashierEncashOrderService cashierEncashOrderService;
    
	public void run() {
		logger.info("提现回推任务现在开始:PlatPushEncashJob");
	    cashierEncashOrderService = getSpringBean("cashierEncashOrderService");
	    
	    int pageNo = 1;
	    int pageSize = 500;
	    int maxPushCount = Configure.getIntValue("job.platpush.maxCount");
	    Date sendTime = DateUtils.addHours(new Date(), -1);// 扫描1小时未回推
	    while(true){
	        List<CashierEncashOrder> orders = cashierEncashOrderService.selectPushOrders(sendTime, maxPushCount, pageNo ++, pageSize);
	        
	        if(orders==null || orders.size() == 0){
	        	logger.info("提现回推任务-->没有需要回推的订单");
	            break;
	        }
	        for(CashierEncashOrder order : orders){
	            try {
	                cashierEncashOrderService.pushEncashOrder(order);
                } catch (Exception e) {
                    logger.error("msg=推送失败 orderId="+(order!=null?order.getOrderId():""), e);
                }
	        }
	    }
	    logger.info("提现回推任务-->PlatPushEncashJob:CashierEncashOrderService执行完成");
	}

}
